package com.itheima;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

public class CodeGenerator {

    @Test
    public void run() {

        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir("E:\\IdeaProjects\\mall-52\\mall-db" + "/src/main/java");

        gc.setAuthor("zhuguo");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖

        //UserServie
        gc.setServiceName("%sService");	//去掉Service接口的首字母I

        gc.setIdType(IdType.AUTO); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式

        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mall?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("1234");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        //下面两行生成包路径就是com.ws.eduservice
        pc.setParent("com.itheima");

        //包下面的controller,service,mapper,entity  com.atguigu.eduservice.controlle//service//mapper//entity
        pc.setEntity("domain");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);


        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setInclude(
                "cms_help",
                "cms_help_category",
                "cms_member_report",
                "cms_prefrence_area",
                "cms_prefrence_area_product_relation",
                "cms_subject",
                "cms_subject_category",
                "cms_subject_comment",
                "cms_subject_product_relation",
                "cms_topic",
                "cms_topic_category",
                "cms_topic_comment",
                "oms_cart_item",
                "oms_company_address",
                "oms_order",
                "oms_order_item",
                "oms_order_operate_history",
                "oms_order_return_apply",
                "oms_order_return_reason",
                "oms_order_setting",
                "pms_album",
                "pms_album_pic",
                "pms_brand",
                "pms_comment",
                "pms_comment_replay",
                "pms_feight_template",
                "pms_member_price",
                "pms_product",
                "pms_product_attribute",
                "pms_product_attribute_category",
                "pms_product_attribute_value",
                "pms_product_category",
                "pms_product_category_attribute_relation",
                "pms_product_full_reduction",
                "pms_product_ladder",
                "pms_product_operate_log",
                "pms_product_vertify_record",
                "pms_sku_stock",
                "sms_coupon",
                "sms_coupon_history",
                "sms_coupon_product_category_relation",
                "sms_coupon_product_relation",
                "sms_flash_promotion",
                "sms_flash_promotion_log",
                "sms_flash_promotion_product_relation",
                "sms_flash_promotion_session",
                "sms_home_advertise",
                "sms_home_brand",
                "sms_home_new_product",
                "sms_home_recommend_product",
                "sms_home_recommend_subject",
                "ums_admin",
                "ums_admin_login_log",
                "ums_admin_permission_relation",
                "ums_admin_role_relation",
                "ums_growth_change_history",
                "ums_integration_change_history",
                "ums_integration_consume_setting",
                "ums_member",
                "ums_member_level",
                "ums_member_login_log",
                "ums_member_member_tag_relation",
                "ums_member_product_category_relation",
                "ums_member_receive_address",
                "ums_member_rule_setting",
                "ums_member_statistics_info",
                "ums_member_tag",
                "ums_member_task",
                "ums_menu",
                "ums_permission",
                "ums_resource",
                "ums_resource_category",
                "ums_role",
                "ums_role_menu_relation",
                "ums_role_permission_relation",
                "ums_role_resource_relation"
        );

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符

        mpg.setStrategy(strategy);


        // 6、执行
        mpg.execute();
    }
    @Test
    public void passWord(){
        System.out.println(SecureUtil.md5("admin"));
    }
}

